<script type="text/javascript">
$(function(){
	// 部署计划详情页：设置要将需要部署的应用分别部署到哪些服务器上
	var pg = App.deploy.planDetail.ui, pp = pg.params,
		leftGd = pg.seek('leftGd'), rightGd = pg.seek('rightGd'),
		appId;
	
	leftGd.mac('grid',{
		cols:[{
			field: 'name', title : '名字', width: 180
		},{
			field: 'type', title : '类型', width: 70,
			render: function(r){
				if (r.type==0){
					return 'JAVA应用';
				} else if(r.status==0){
					return '库包应用';
				}
			}
		},{
			field: 'version', title : '版本号', width: 120, sort: true,
		},{
			field: 'code', title : '应用代码', width: 150
		}],
	 	loader: {
			url: App.deploy.ctx + '/plan/item/list2.do',
			params: {
				pageNo: 1, pageSize: 10,
				planId: pp.id, projId: pp.projId
			},
			autoLoad: true
		},
		pagerLength: 10,
		onRowClick: function(){
			var tr = $(this), sr = leftGd.selected;
			if (tr.hasClass('selected'))
				return true;
			if (sr)
				sr.removeClass('selected');
			sr = leftGd.selected = tr.addClass('selected');
			var k = sr.attr('name'), d = leftGd.data[k];
			pg.seek('target').removeAttr('disabled');
			rrid = d.targetId;
			appId = d.id;
			rightGd.load({
				planId: pp.id, targetId: rrid,
				env: envCb.val(), pageNo: 1
			});
		} 
	});
	rightGd.mac('grid',{
		cols:[{
			field: 'name', title : '节点名称', width: 180
		},{
			field: 'ip', title : 'IP地址', width: 100
		},{
			field: 'env', title : '环境', width: 60
		},{
			field: 'id', title : '操作', width: 80,
			render: function(r){
				return $('<span class="a">移除</span>').click(function(){
					$.post(App.deploy.ctx + '/plan/detail/delete.do', {
						id: r.id
					},function(){
						rightGd.load()
					});
				});
			}
		}],
		loader: {
			url: App.deploy.ctx + '/plan/detail/list.do',
			params: { pageNo: 1, pageSize: 10 },
			autoLoad: false
		},
		pagerLength: 10
	});

	var envCb = pg.seek('envCb');
	envCb.mac('combo', {
		//boxLeft:300,
		width: 70,
		boxWidth: 71,
		keyField: 'k',
		displayField: 'v',
		data: Cache.base.env('deploy'),
		onSelect: function(me, sk, sd){
			rightGd.load({ env: sk[0] });
		}
	}).val('');
	
	pg.seek('target').click(function(){
		App.open({
			ctx: 'base',
			title: '应用节点表',
			action: 'serverPicker',
			model: true,
			params: {
				planId: pp.id, id: rrid, multi: 1,
				env: envCb.val()
			}
		}, function(s){
			var a = $.map(s, function(n){
				return n.id;
			});
			alert(JSON.stringify(a));
			$.post(App.deploy.ctx + '/plan/detail/save.do', {
				detailId: appId,
				planId: pp.id,
				targetId: rrid,
				serverIds: a.join(',')
			}, function(rs){
				rightGd.load();
			});
		});
	});
	pg.seek('prev').click(function(){
		App.show({
			ctx: 'deploy',
			action: 'planItem',
			params: pp
		}, $.noop, pg.parent());
	});
	pg.seek('next').click(function(){
		App.show({
			ctx: 'deploy',
			action: 'packIndex',
			params: pp
		}, $.noop, pg.parent());
	});
	pg.children('.name').click(function(){
		 App.open({
			title: '部署计划基本信息',
			ctx: 'deploy',
			action: 'planEdit',
			model: true,
			params: pp
		}); 
	}).html(pp.name);
	function adjust(){
		var p = pg.parent(), h = p.height();
		pg.height(h-44);
		leftGd.parent().height(h-210);
		leftGd.adjust();
		rightGd.parent().height(h-210);
		rightGd.adjust()
	}
	$(window).resize(adjust);
	adjust();
});
</script>
<h1 class="name"></h1>
<div class="body">
	<div class="left">
		<div class="head">
			&nbsp;
		</div>
		<div class="clear"></div>
		<div class="body">
			<div class="grid" name="leftGd"></div>
		</div>
	</div>
	<div class="right">
		<div class="head">
			<button name="target" disabled="disabled">添加待部署服务器</button>
			<div class="combo fr" name="envCb"></div>
		</div>
		<div class="clear"></div>
		<div class="body">
			<div class="grid" name="rightGd"></div>
		</div>
	</div>
	<div class="clear"></div>
</div>
<div class="foot">
	<button name="prev">上一步</button>
	<button name="next">下一步</button>
</div>
